<?php
/**
 * Created by PhpStorm.
 * User: lantranduc
 * Date: 5/22/14
 * Time: 1:54 PM
 */
App::uses('AppController', 'Controller');
class UsersController extends AppController{





    public function beforeFilter() {
        parent::beforeFilter();
        if (in_array($this->action, array('index','myLink'))) {
            if(!$this->Session->check('user')){
                $this->redirect(array('controller'=>'Users','action'=>'login/'.$this->action));
            }
        }
        if (in_array($this->action, array('login','register'))) {
            if($this->Session->check('user')){
                $this->redirect(array('controller'=>'Users','action'=>'index'));
            }

        }
    }


    public  function index(){
        pr( $this->User->find('all'));
    }

    public function login($redirect=null){
        $this->set(array('redirect'=>$redirect));
        if($this->request->is('post')){
            $input=$this->request->data;
            if(filter_var($input['email'],FILTER_VALIDATE_EMAIL) && strlen($input['password'])>=6){
                $result=$this->User->find('first',array(
                    'conditions'=>array(
                                            'User.email'=>$input['email'],
                                            'User.type'=>array(1,2),
                                            'User.password'=>md5($input['password']),
                                        ),
                    'recursive'=>0,
                ));
                if(count($result)>0){
                    $this->Session->write('user',$result);
                    if($redirect=='mylink'){
                        $this->redirect(array('controller'=>'Users','action'=>'myLink'));
                    }elseif($redirect=='index'){
                        $this->redirect(array('controller'=>'Users','action'=>'index'));
                    }else{
                        $this->redirect(array('controller'=>'pages','action'=>'display'));
                    }


                }else{
                   $this->set(array('err'=>'style="border-color:red;"'));
                    $this->set(array('msg'=>'User name and password enter did not match!'));
                }


            }else{
                $this->set(array('err'=>'style="border-color:red;"'));
                $this->set(array('msg'=>'User name and password enter did not match!'));
            }

        }



    }

    public function logout(){
        if($this->Session->check('user')){
            $this->Session->destroy();
            $this->redirect(array('controller'=>'pages','action'=>'display'));
        }
    }

    public function myLink(){
        $user=$this->Session->read('user');
        $result=$this->User->find('all',array(
            'conditions'=>array(
                'User.email'=>$user['User']['email'],
                'User.type'=>$user['User']['type'],
                'User.userID'=>$user['User']['userID'],
            ),
            'recursive'=>1,
        ));

        $this->set(array('links'=>$result[0]['links']));

    }



    public function register(){
        $this->set('title_for_layout','Register');
        $msg=array();
        $msg['email']='';
        $msg['re-email']='';
        $msg['password']=$msg['re-password']='';
        $msg['captcha']='style="height:50px;"';


        if($this->request->is('post')){
            $data=$this->request->data;
            $check=true;
            if($data['email']!=$data['re-email'] || !filter_var($data['email'],FILTER_VALIDATE_EMAIL) ){
                $msg['re-email']=$msg['email']='style="border-color:red;"';
                $check=false;
                $msg['msg']='Email and re-email not Equal or not valid!<br/>';
            }elseif($this->User->find('count',array('conditions'=>array('User.email'=>$data['email']),'recursive'=>0))!=0){
                $msg['re-email']=$msg['email']='style="border-color:red;"';
                $check=false;
                $msg['msg']='Email unavailable!<br/>';
            }
            if($data['password']!=$data['re-password'] || strlen($data['password'])<6 ){
                $msg['re-email']=$msg['email']='style="border-color:red;"';
                $check=false;
                $msg['msg']='Password and re-password not equal or smaller 6 characters!<br/>';
            }
            if(!$this->Captcha->checkCaptcha($data['captcha'])){
                $msg['captcha']='style="border-color:red;!important;height:50px;"';
                $check=false;
                $msg['msg']='Captcha did not match!<br/>';
            }
            if(!$check){
                $msg['msg']='Something got wrong!<br/>'.$msg['msg'];
                unset($check);
            }else{
                $this->User->set(array('email'=>$data['email'],'password'=>md5($data['password']),'type'=>2));
                $this->User->save();
                if($this->User->find('count',array('conditions'=>array('User.email'=>$data['email'],'password'=>md5($data['password']),'type'=>2)))==1){
                    $this->Session->write('user',$this->User->find('first',array('conditions'=>array(
                        'User.email'=>$data['email'],
                        'User.type'=>2,
                        'User.password'=>md5($data['password'])
                    ))));
                    $this->redirect(array('controller'=>'pages','action'=>'display'));
                }else{
                    $msg['msg']='Something got Wrong!<br/>We cant create your account right now, Please contact administator for more information !';
                }
            }
        }
        $this->set('msg',$msg);
        unset($data);
        unset($msg);
    }
}